package top.minuy.structure.map;

import top.minuy.res.book.BookRes;
import top.minuy.util.generate.ArrayGenerator;
import top.minuy.util.helper.FileHelper;
import top.minuy.structure.linear.array.Array;

/**
 * 测试
 * 链表映射 2021.9.11
 * 二分搜索树映射 2021.9.12
 * AVL树映射 2021.11.4
 * 红黑树映射 2021.11.6
 *
 * @author Minuy
 * @time 21:53
 * @date 2021/9/11
 */
public class Test {
    public static void main(String[] args) {
        testWriteAndRead();
        // testWMain();
    }

    public static void testWMain(){
        int n;
        Integer[] s;
        BSTMap<Integer, Integer> bstMap;
        AVLTreeMap<Integer, Integer> avlTreeMap;
        RBTreeMap<Integer, Integer> rbTreeMap;

        n = 20000000;
        avlTreeMap = new AVLTreeMap<>();
        rbTreeMap = new RBTreeMap<>();
        System.out.println("Data = Ordered, TestLoad = "+n);
        s = ArrayGenerator.generateOrderedArray(n);

        System.out.println("RBT : ");
        testWrite(s,rbTreeMap);
        System.out.println("AVL : ");
        testWrite(s,avlTreeMap);


        n = 2000000;
        bstMap = new BSTMap<>();
        avlTreeMap = new AVLTreeMap<>();
        rbTreeMap = new RBTreeMap<>();
        System.out.println("Data = Random, TestLoad = "+n);
        s = ArrayGenerator.generateRandomArray(n,n);

        System.out.println("BST : ");
        testWrite(s,bstMap);
        System.out.println("AVL : ");
        testWrite(s,avlTreeMap);
        System.out.println("RBT : ");
        testWrite(s,rbTreeMap);

    }

    public static void testWrite(Integer[] data,Map<Integer, Integer> dict){
        long startTime = System.nanoTime();
        for (Integer i : data){
            dict.add(i,100);
        }
        long endTime = System.nanoTime();
        System.out.println("time = "+((endTime-startTime)/1000000000.0)+" s");
    }

    public static void testWriteAndRead(){
        double d1 = 0, d2 = 0, d3 = 0, d4 = 0;
        LinkedListMap<String, Integer> linkedListMap = new LinkedListMap<>();
        BSTMap<String, Integer> bstMap = new BSTMap<>();
        AVLTreeMap<String, Integer> avlTreeMap = new AVLTreeMap<>();
        RBTreeMap<String, Integer> rbTreeMap = new RBTreeMap<>();

        System.out.println("BST : Map");
        d1 = testWRMain(bstMap);

        System.out.println("LinkedList : Map");
        d2 = testWRMain(linkedListMap);

        System.out.println("AVLTree : Map");
        d3 = testWRMain(avlTreeMap);

        System.out.println("RedBlackTree : Map");
        d4 = testWRMain( rbTreeMap);


        System.out.println(
                "AVLTree : isBST = " + avlTreeMap.isBST()
                        + ", isBalanced = " + avlTreeMap.isBalanced()
        );
        System.out.println("t1 = " + d1 + ", t2 = " + d2 + ", t3 = " + d3 + ", t4 = " + d4);

        System.out.println();
        System.out.println("取出测试：");

        Array<String> words = getWords(BookRes.PRIDE_AND_PREJUDICE);

        boolean isRight = true;
        for (int i = 0; i < words.getSize(); i++) {
            bstMap.remove(words.get(i));
            if (!bstMap.isBST()) {
                isRight = false;
                break;
            }
        }

        System.out.println("BSTMap : " + isRight);

        isRight = true;

        for (int i = 0; i < words.getSize(); i++) {
            avlTreeMap.remove(words.get(i));
            if ((!avlTreeMap.isBST()) || (!avlTreeMap.isBalanced())) {
                isRight = false;
                break;
            }
        }

        System.out.println("AVLTreeMap : " + isRight);
    }

    public static double testWRMain(Map<String, Integer> dict) {
        Array<String> words = getWords(BookRes.PRIDE_AND_PREJUDICE);

        long startTime = System.nanoTime();
        // 加入
        for (int i = 0; i < words.getSize(); i++) {
            String word = words.get(i);
            if (dict.contains(word)) {
                dict.set(word, dict.get(word) + 1);
            } else {
                dict.add(word, 1);
            }
        }
        long endTime = System.nanoTime();
        System.out.println();

        System.out.println("Total word : " + words.getSize());
        System.out.println("Total different word : " + dict.getSize());
        System.out.println("The frequency of PRIDE : " + dict.get("pride"));
        System.out.println("The frequency of PREJUDICE : " + dict.get("prejudice"));
        System.out.println();

        return (endTime - startTime) / 1000000000.0;
    }

    public static Array<String> getWords(String fileName) {
        Array<String> words = new Array<>();
        FileHelper.readFile(fileName, words);
        return words;
    }
}

/*
2021.11.6 测试结果
"C:\Program Files\JetBrains\IntelliJ IDEA 2019.3.5\jbr\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.3.5\lib\idea_rt.jar=6473:C:\Program Files\JetBrains\IntelliJ IDEA 2019.3.5\bin" -Dfile.encoding=UTF-8 -classpath D:\Project\DataStructureJavaLearn2021\out\production\data_structure_java top.minuy.structure.map.Test

-> ts.Test : BST : Map
Total word : 126046
Total different word : 6524
The frequency of PRIDE : 52
The frequency of PREJUDICE : 10


-> ts.Test : LinkedList : Map
Total word : 126046
Total different word : 6524
The frequency of PRIDE : 52
The frequency of PREJUDICE : 10


-> ts.Test : AVLTree : Map
Total word : 126046
Total different word : 6524
The frequency of PRIDE : 52
The frequency of PREJUDICE : 10


-> ts.Test : RedBlackTree : Map
Total word : 126046
Total different word : 6524
The frequency of PRIDE : 52
The frequency of PREJUDICE : 10

AVLTree : isBST = true, isBalanced = true
t1 = 0.1454265, t2 = 15.1512033, t3 = 0.124479301, t4 = 0.1944643

取出测试：
BSTMap : true
AVLTreeMap : true

Process finished with exit code 0

 */


